package com.example.demo.jenkins;

import com.example.demo.pojo.ModelFactory;
import com.example.demo.utils.LoggerConfig;
import com.offbytwo.jenkins.JenkinsServer;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class JobApi {

    private JenkinsServer jenkinsServer;

    private JobApi(){
        jenkinsServer = ModelFactory.INSTANCE.getJenkinsServer();
    }

    /**
     * 指定job构建
     * @param jobName 传入jobName
     */
    private void buildJob(String jobName){
        try {
            jenkinsServer.getJob(jobName).build();
        } catch (IOException e) {
            LoggerConfig.getLoggerError("buildJob:未获取到指定的job");
            e.printStackTrace();
        }
    }

    /**
     *
     * @param key 参数名称，需要在Jenkins上设置
     * @param value 参数的值
     * @param jobName 任务名称
     */
    private void buildParamJob(String key , String value,String jobName){
        Map<String,String> map = new HashMap<>();
        map.put(key,value);
        try {
            jenkinsServer.getJob(jobName).build(map);
        } catch (IOException e) {
            LoggerConfig.getLoggerError("buildParamJob:未获取到指定的job");
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        JobApi jobApi = new JobApi();
        jobApi.buildJob("test");
//        jobApi.buildParamJob("ev","1111","maven_test");
    }
}
